package com.iaz.tech.tools.uml.file.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iaz.tech.tools.uml.IUMLNeuronAble;
import com.iaz.tech.tools.uml.structural.classstruct.impl.NeuronFunctionStruct;
import com.iaz.tech.tools.uml.structural.classstruct.impl.NeuronInterfaceStruct;
import com.iaz.tech.tools.uml.structural.classstruct.impl.NeuronParameterStruct;

import lombok.Data;
import lombok.EqualsAndHashCode;

@EqualsAndHashCode(callSuper=false)
@Data
public class NeuronInterfaceLoader extends NeuronParameterStructLoader  {
    
    /**
     * 日志
     */
	private final static Logger logger = (Logger) LoggerFactory.getLogger(NeuronInterfaceLoader.class);
    

    @Override
    public NeuronParameterStruct toParameterStruct(IUMLNeuronAble clazz) {
        if (clazz == null) {
            throw new IllegalArgumentException("umlObj 不能为空");
        }
        if (!(clazz instanceof NeuronInterfaceStruct)){
            throw new IllegalArgumentException("umlObj 必须是 NeuronInterfaceStruct 类型");
        } 
        NeuronInterfaceStruct clazz_struct = (NeuronInterfaceStruct) clazz;
        return clazz_struct;
    }
    
    @Override
    public void parseJSON(IUMLNeuronAble umlObj, JSONObject json) {
        try {
            if (json == null) {
                throw new Exception("JSON字符串为空"); 
            }
            if (umlObj == null){
                throw new Exception("要解析的UML对象为空"); 
            }
            if (!(umlObj instanceof NeuronInterfaceStruct)){
                throw new Exception("要解析的UML对象不是NeuronInterfaceStruct类型"); 
            }
            if (json.containsKey("uml")) {
                String jsonUML = json.getString("uml"); 
                if (!jsonUML.equals("InterfaceStruct")) {
                    return ;
                }
            }

            NeuronInterfaceStruct neuron_class_struct = (NeuronInterfaceStruct)umlObj;
            
            super.parseJSON(neuron_class_struct, json);
            
            if (json.containsKey("functions")) {
                JSONArray json_array = json.getJSONArray("functions");
                if (json_array != null) {
                    for (int i = 0; i < json_array.size(); i++) {
                        JSONObject json_obj = json_array.getJSONObject(i);
                        if (json_obj == null) {
                            logger.error("UML 配置文件出现错误，在第" + i + "个函数配置为空。");
                            continue;
                        }
                        IUMLNeuronAble param = UMLLoaderFactory.generateUML(json_obj);   
                        if (!(param instanceof NeuronFunctionStruct)) {
                            logger.error("UML 配置文件出现错误，在第" + i + "个函数配置不是NeuronFunctionStruct类型。");
                            continue;
                        }                         
                        if (param != null) {
                            neuron_class_struct.getFunctions().add((NeuronFunctionStruct)param);
                        }                           
                    }
                }                 
            }
        } catch (Exception e) {
            logger.error("解析JSON字符串失败", e);  
        }  
    }
    @Override
    public JSONObject toJSON(IUMLNeuronAble classStruct) {
        JSONObject result = new JSONObject();
        try {
            if (classStruct == null) {
                throw new Exception("要转换的UML对象为空"); 
            }
            if (!(classStruct instanceof NeuronInterfaceStruct)){
                throw new Exception("要转换的UML对象不是NeuronInterfaceStruct类型"); 
            }
            result = super.toJSON(classStruct);
            result.put("uml", "InterfaceStruct");
            
            JSONArray _functions = new JSONArray();
            NeuronFunctionLoader functionLoader = new NeuronFunctionLoader();
            for (NeuronFunctionStruct param : ((NeuronInterfaceStruct)classStruct).getFunctions()) {
                JSONObject param_json_string = functionLoader.toJSON(param);
                if (param_json_string == null) {
                    continue;
                }
                _functions.add( param_json_string );
            }
            result.put("functions", _functions); 
        } catch (Exception e) {
            logger.error("转换为JSON字符串失败", e); 
            result = new JSONObject();
        }
        return result;
    }

}
